﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using WaterBill.DTO;

namespace WaterBill.DAL
{
    class DAL_Users : clsConnection
    {
        DataTable dtU;

        //Kiem tra dang nhap
        public DataTable DAL_Users_checKLogin(string UserName, string Password)
        {
            dtU = new DataTable();
            AddParameter(new SqlParameter("@UserName", UserName.Trim()));
            AddParameter(new SqlParameter("@PassWord", DAL_Md5(Password.Trim())));
            dtU = ExcuteQuery("checkLogin");
            return dtU;
        }

        //Kiem tra ton tai ten dang nhap
        public Boolean DAL_Users_CheckExist(string UserName)
        {
            dtU = new DataTable();
            AddParameter(new SqlParameter("@UserName", UserName.Trim()));
            dtU = ExcuteQuery("CheckExistUser");
            if (dtU.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }

        //Them mot nguoi dung moi
        public Boolean DAL_Users_Insert(DTO_Users obj)
        {
            try
            {
                AddParameter(new SqlParameter("@UserName", obj.UserName.Trim()));
                AddParameter(new SqlParameter("@FullName", obj.FullName.Trim()));
                AddParameter(new SqlParameter("@Address", obj.Address.Trim()));
                AddParameter(new SqlParameter("@Phone", obj.Phone.Trim()));
                AddParameter(new SqlParameter("@PermissionId", obj.PermissionId));
                AddParameter(new SqlParameter("@PassWord", DAL_Md5(obj.UserName.Trim())));
                AddParameter(new SqlParameter("@Status", obj.Status));
                if (ExcuteNonQuery("InsertUser"))
                {
                    return true;
                }
            }
            catch
            {
            }
            return false;
        }

        //Sua nguoi dung
        public Boolean DAL_Users_Update(DTO_Users obj)
        {
            try
            {
                AddParameter(new SqlParameter("@UserId", obj.UserId));
                AddParameter(new SqlParameter("@FullName", obj.FullName.Trim()));
                AddParameter(new SqlParameter("@Address", obj.Address.Trim()));
                AddParameter(new SqlParameter("@Phone", obj.Phone.Trim()));
                AddParameter(new SqlParameter("@PermissionId", obj.PermissionId));
                AddParameter(new SqlParameter("@Status", obj.Status));
                if (ExcuteNonQuery("UpdateUser"))
                {
                    return true;
                }
            }
            catch
            {
            }
            return false;
        }

        //Doi mat khau nguoi dung
        public Boolean DAL_Users_ChangePassword(DTO_Users obj)
        {
            try
            {
                AddParameter(new SqlParameter("@UserId", obj.UserId));
                AddParameter(new SqlParameter("@PassWord", DAL_Md5(obj.PassWord.Trim())));
                if (ExcuteNonQuery("ChangePassword"))
                {
                    return true;
                }
            }
            catch
            {
            }
            return false;
        }

        //Lay danh sach tat ca nguoi dung
        public DataTable DAL_Users_GetAll()
        {
            dtU = new DataTable();
            try
            {
                dtU = ExcuteQuery("GetAllUser");
            }
            catch
            {
            }
            return dtU;
        }

        //Lay danh sach nguoi dung dua vao quyen
        public DataTable DAL_Users_GetByPermission(int PermissionId)
        {
            dtU = new DataTable();
            try
            {
                AddParameter(new SqlParameter("@PermissionId", PermissionId));
                dtU = ExcuteQuery("GetUserByPermissionId");
            }
            catch
            {
            }
            return dtU;
        }

        //Lay danh sach nguoi dung dua vao quyen
        public DataTable DAL_Users_GetByUserId(int UserId)
        {
            dtU = new DataTable();
            try
            {
                AddParameter(new SqlParameter("@UserId", UserId));
                dtU = ExcuteQuery("GetUserByUserId");
            }
            catch
            {
            }
            return dtU;
        }

        //Xoa nguoi dung
        public Boolean DAL_Users_Delete(int UserId)
        {
            try
            {
                AddParameter(new SqlParameter("@UserId", UserId));

                if (ExecuteScalar("DeleteUser") == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
            }
            return false;
        }

        public string DAL_Md5(string str)
        {
            Byte[] dauvao = ASCIIEncoding.Default.GetBytes(str);
            using (MD5 md5 = new MD5CryptoServiceProvider())
            {
                var daura = md5.ComputeHash(dauvao);
                return BitConverter.ToString(daura);
            }
        }
    }
}
